package com.gmail.avereshchaga.cashierdeskconnectimpl;

import com.gmail.avereshchaga.cashierdeskconnect.CashierDeskConnect;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Form;
import org.apache.http.client.fluent.Request;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.openide.util.lookup.ServiceProvider;

/**
 *
 * @author <a href="mailto:avereshchaga@gmail.com">Vereshchaga Alexey</a>
 */
@ServiceProvider(service = CashierDeskConnect.class)
public class CashierDescConnectImpl implements CashierDeskConnect {

    private String sessionId;
    private String accessLevel;
    private String host;

    /**
     * Calls the server using a user name and password to get information about
     * the session
     *
     * @param host host address
     * @param username user name for which the session is created
     * @param password password of the user
     * @return information about established connection
     * @throws IOException
     */
    @Override
    public String callServer(String host, String username, String password) throws IOException {
        String connectionURLFull = "http://" + host + "/connection";
        ObjectMapper mapper = new ObjectMapper();
        String JSONString = Request.Post(connectionURLFull)
                .bodyForm(Form.form().add("username", username).add("password", password).build())
                .execute().returnContent().asString();

        List<String> sessionIdInfo = mapper.readValue(JSONString, new TypeReference<ArrayList<String>>() {
        });
        if (sessionIdInfo.get(0).equals("Success Connection")) {
            sessionId = sessionIdInfo.get(1);
            accessLevel = sessionIdInfo.get(2);
            this.host = host;
            return sessionIdInfo.get(0);
        } else {
            return sessionIdInfo.get(0);
        }
    }

    /**
     * Close all sessions for current user
     *
     * @param username user name of the current user
     * @param password password of the current user
     * @throws ClientProtocolException
     * @throws IOException
     */
    @Override
    public void closeSession(String username, String password) throws ClientProtocolException, IOException {
        String connectionURLFull = "http://" + host + "/closeSession";
        Request.Post(connectionURLFull)
                .bodyForm(Form.form().add("username", username).add("password", password).build())
                .execute();
    }

    @Override
    public String getSessionId() {
        return sessionId;
    }

    @Override
    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    @Override
    public String getAccessLevel() {
        return accessLevel;
    }

    @Override
    public void setAccessLevel(String accessLevel) {
        this.accessLevel = accessLevel;
    }

    @Override
    public String getHost() {
        return host;
    }

    @Override
    public void setHost(String host) {
        this.host = host;
    }
}
